import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'

// 读取本地存储数据
var car = JSON.parse(localStorage.getItem('car') || '[]');

import Vuex from 'vuex'
Vue.use(Vuex)
var store = new Vuex.Store({
    state: { // this.$store.state.***
        car: car
    },
    mutations: { // this.$store.commit('方法名', '唯一一个参数')
        addToCar(state, goodsinfo) {
            var flag = false;
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count);
                    flag = true;
                    return true;
                }
            });
            if (!flag) {
                state.car.push(goodsinfo);
            }
            // 把car数组存储到本地的localStorage中
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        updateGoodsInfo(state, goodsinfo) {
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count = parseInt(goodsinfo.count);
                    return true;
                }
            });
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        removeFromCar(state, id) {
            state.car.some((item, i) => {
                if (item.id == id) {
                    state.car.splice(i, 1);
                    return true;
                }
            });
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        updateGoodsSelected(state, info) {
            state.car.some(item => {
                if (item.id == info.id) {
                    item.selected = info.selected;
                }
            });
            localStorage.setItem('car', JSON.stringify(state.car));
        }
    },
    getters: { // this.$store.getters.***
        getAllCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += item.count;
            })
            return c;
        },
        getGoodsCount(state) {
            var o = {};
            state.car.forEach(item => {
                o[item.id] = item.count;
            })
            return o;
        },
        getGoodsSelected(state) {
            var o = {};
            state.car.forEach(item => {
                o[item.id] = item.selected;
            })
            return o;
        },
        getGoodsCountAndAmount(state) {
            var o = {
                count: 0,
                amount: 0
            };
            state.car.forEach(item => {
                if (item.selected) {
                    o.count += item.count;
                    o.amount += item.price * item.count;
                }
            })
            return o;
        }
    }
})

import axios from 'axios'
axios.defaults.baseURL = 'http://www.liulongbin.top:3005'; // 配置全局基地址
Vue.prototype.$axios = axios;

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

/* import { Header, Swipe, SwipeItem, Button, Lazyload } from 'mint-ui'
import 'mint-ui/lib/style.css' // 一定要导入mint-ui的样式
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.use(Lazyload); // 注意这里是use */

// 按需导入无法实现懒加载效果
import MintUi from 'mint-ui'
Vue.use(MintUi);
import 'mint-ui/lib/style.css'


import moment from 'moment'
Vue.filter('dataFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return moment(dataStr).format(pattern);
})

import VuePreview from 'vue-preview'
Vue.use(VuePreview)


import app from './App.vue'


var vm = new Vue({
    el: '#app',
    render: c => c(app),
    // render: function(createElement) { // 完整写法
    //     return createElement(app);
    // },
    router,
    store
})